#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "AD.h"
#include "Thermistor.h"
#include <math.h>

uint16_t ADValue;
float Voltage;

// 打印出温度传感器的提示信息
void makerobo_Print(int x)
{
	switch(x)
	{
		case 0:  // 太热
			OLED_ShowString(2,1,"****************" );
			OLED_ShowString(3,1,"**Temp Too Hot!*" );
			OLED_ShowString(4,1,"****************" );
		break;
		case 1: // 正合适
			OLED_ShowString(2,1,"*****************" );
			OLED_ShowString(3,1,"*Temp Better~ **"  );
			OLED_ShowString(4,1,"******************");
		break;
		default: // 打印错误信息
			OLED_ShowString(2,1,"*****************" );
			OLED_ShowString(3,1,"Print value error.");
			OLED_ShowString(4,1,"*****************" );
		break;
	}
}

int main(void)
{
	// 相关变量定义
	unsigned char T_analogVal;
	double T_Vr, T_Rt, T_temp;
	int T_tmp, T_status;
	
	OLED_Init();
	AD_Init();
	Thermistor_Init();
	
	OLED_ShowString(1,1,"T_temp:");
	OLED_ShowString(1,14,"C");
	
	while(1)
	{
		T_analogVal = AD_GetValue();
		
		T_Vr = 3.3 * (double)(T_analogVal) / 4095;
		T_Rt = 10000 * (double)(T_Vr) / (3.3 - (double)(T_Vr));
		T_temp = 1 / (((log(T_Rt/10000)) / 3950)+(1 / (273.15 + 25)));
		T_temp = T_temp - 273.15;
		
		OLED_ShowNum(1,9,T_temp,4);
		
		T_tmp =  Thermistor_Get();      // 读取温度传感器数字管脚
		if(T_tmp != T_status)           // 判断状态值发生改变
		{
			makerobo_Print(T_tmp);   // 打印出温度传感器的提示信息
			T_status = T_tmp;        // 把当前状态值设置为比较状态值，避免重复打印；	
		}
		Delay_ms(100);
	}
}

